package com.gs.api.util.gen;

import java.text.DecimalFormat;

import java.text.FieldPosition;

import java.text.Format;

import java.text.NumberFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

/**
 * @author guoyunfei on 2021/10/29 12:35
 * @className GenerateSequenceUtil
 * @description TODO
 * @Version 1.0
 */
public class GenerateSequenceUtil {


    /** The FieldPosition. */

    private static final FieldPosition HELPER_POSITION = new FieldPosition(0);

    /** This Format for format the data to special format. */

    private final static Format dateFormat = new SimpleDateFormat("yyyyMMddHHmmssS");

    /** This Format for format the number to special format. */

    private final static NumberFormat numberFormat = new DecimalFormat("00");

    /** This int is the sequence number ,the default value is 0. */

    private static int seq = 0;

    private static final int MAX = 99;

    /**

     * 时间格式生成序列

     * @return String

     */

    public static synchronized String generateSequenceNo() {
        Calendar rightNow = Calendar.getInstance();
        StringBuffer sb = new StringBuffer();
        dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);
        //numberFormat.format(seq, sb, HELPER_POSITION);
        if (seq == MAX) {
            seq = 0;
        } else {
            seq++;
        }
        System.out.println("THE SQUENCE IS :" + sb.toString());
        return sb.toString();
    }


    public static void main(String[] args) {
        generateSequenceNo();
        generateSequenceNo();
    }

}
